layui.use(['carousel', 'form'], function(){
    var carousel = layui.carousel
        ,form = layui.form;

    //常规轮播
    carousel.render({
        elem: '#test1'
        ,arrow: 'always'
    });

    //改变下时间间隔、动画类型、高度
    carousel.render({
        elem: '#test2'
        ,interval: 1800
        ,anim: 'fade'
        ,height: '120px'
    });

    //设定各种参数
    var ins3 = carousel.render({
        elem: '#test3'
    });
    //图片轮播
    carousel.render({
        elem: '#test10'
        ,width:'900px'
        ,height:'440px'
        ,interval: 5000
    });

    //事件
    carousel.on('change(test4)', function(res){
        console.log(res)
    });

    var $ = layui.$, active = {
        set: function(othis){
            var THIS = 'layui-bg-normal'
                ,key = othis.data('key')
                ,options = {};

            othis.css('background-color', '#5FB878').siblings().removeAttr('style');
            options[key] = othis.data('value');
            ins3.reload(options);
        }
    };

    //监听开关
    form.on('switch(autoplay)', function(){
        ins3.reload({
            autoplay: this.checked
        });
    });

    $('.demoSet').on('keyup', function(){
        var value = this.value
            ,options = {};
        if(!/^\d+$/.test(value)) return;

        options[this.name] = value;
        ins3.reload(options);
    });

    //其它示例
    $('.demoTest .layui-btn').on('click', function(){
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });
});

var vm = new Vue({
        el:"#app",
        data:{
            //类型数组
            newsList:[],
            strategyList:[],
            gameList:[],
            userName:null
        },
        //页面加载完成之后执行的方法
        mounted:function () {
            this.userName=window.localStorage.getItem("username");
            axios.get("/article/getAllNews")
                .then(function(res){
                    vm.newsList = res.data.data;
                })
            axios.get("/article/getAllStrategy")
                .then(function(res){
                    vm.strategyList = res.data.data;
                })
            axios.get("/game/getAllGameTypes")
                .then(function(res){
                    vm.gameList = res.data.data;
                })
        },
        methods:{
            gotoNews:function (news) {
                //使用localStorage传参(类型的id)
                // window.localStorage.setItem("tid",type.tid);
                // window.localStorage.setItem("tname",type.tname);
                //使用localStorage传递json对象: 先把json对象转换为字符串，然后传递字符串
                //localStorage传参：是根据字符串传参

                window.localStorage.setItem("news",JSON.stringify(news));
                //页面跳转到list.html
                window.location.href = "/pages/news.html";
            },
            gotoStrategy:function (strategy) {
                //使用localStorage传参(类型的id)
                // window.localStorage.setItem("tid",type.tid);
                // window.localStorage.setItem("tname",type.tname);
                //使用localStorage传递json对象: 先把json对象转换为字符串，然后传递字符串
                //localStorage传参：是根据字符串传参
                window.localStorage.setItem("strategy",JSON.stringify(strategy));
                //页面跳转到list.html
                window.location.href = "/pages/strategy.html";

            },gotoGameDetail:function(game){
                window.localStorage.setItem("game",JSON.stringify(game));
                window.location.href = "/pages/gameDetail.html";
            },
            gotoGame:function () {
                var game = this.$refs.game.value;
                window.localStorage.setItem("gameName",JSON.stringify(String));
                window.location.href = "/pages/gameDetail.html";
            },
            logout:function(){
                window.localStorage.removeItem("username");
                window.location.href = "/pages/index.html";
            },
            getGames:function(type){
                this.$http
                    .post('/game/getGameByType',type)
                    .then(function(res){
                        vm.gameList=res.data.data;
                    })
            },
            // getTime:function(i,length){
            //     this.$http
            //         .post('/article/getNewsArticle',this.newsList[i])
            //         .then(function(res){
            //             // this.len2++;
            //             vm.articleList.push(res.data.data);
            //             if (++i < length){
            //                 vm.getTime(i,length)
            //             }
            //         })
            // },
            formatDateTime : function(time) {
                if(time!=null){
                    var datetime = new Date();
                    datetime.setTime(time);
                    var year = datetime.getFullYear();
                    var month = (datetime.getMonth() + 1)<10?"0"+(datetime.getMonth() + 1):(datetime.getMonth() + 1);
                    var date = datetime.getDate()<10?"0"+datetime.getDate():datetime.getDate();
                    var hour = datetime.getHours()<10?"0"+datetime.getHours():datetime.getHours();
                    var minute = datetime.getMinutes()<10?"0"+datetime.getMinutes():datetime.getMinutes();
                    var second = datetime.getSeconds()<10?"0"+datetime.getSeconds():datetime.getSeconds();
                    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
                }else{
                    return "---";
                }
            },formatDate : function(time) {
                if(time!=null){
                    var datetime = new Date();
                    datetime.setTime(time);
                    var year = datetime.getFullYear();
                    var month = (datetime.getMonth() + 1)<10?"0"+(datetime.getMonth() + 1):(datetime.getMonth() + 1);
                    var date = datetime.getDate()<10?"0"+datetime.getDate():datetime.getDate();
                    var hour = datetime.getHours()<10?"0"+datetime.getHours():datetime.getHours();
                    var minute = datetime.getMinutes()<10?"0"+datetime.getMinutes():datetime.getMinutes();
                    var second = datetime.getSeconds()<10?"0"+datetime.getSeconds():datetime.getSeconds();
                    return year + "-" + month + "-" + date;
                }else{
                    return "---";
                }
            }

        },
    }

    )

